package cc.idoone.spring_boot_hello.mongodb.controller;

import cc.idoone.spring_boot_hello.mongodb.bean.MongoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * MongoDB 使用
 *
 * @author hemj
 * @since 2017/10/20
 */
@RestController
@RequestMapping("/mongodb")
public class MongoController {

	@Autowired
	private MongoTemplate mongoTemplate;

	@GetMapping("/new")
	public String newBean() {
		MongoBean bean = new MongoBean();
		bean.setUsername("username");
		bean.setPassword("123456");
		bean.setEmail("163.com");
		bean.setLocked(true);
		bean.setTs(new Date());
		mongoTemplate.save(bean);

		return "success";
	}

	@GetMapping("/findAll")
	public List<MongoBean> findAll() {
		return mongoTemplate.findAll(MongoBean.class);
	}
}
